﻿using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using Bookvar.ObjectModel;
using Bookvar.ServiceModel;

namespace Bookvar.AddIn
{
    public class AddInService : IAddinService
    {
        private readonly string addInRoot = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location );

        public IEnumerable<T> LoadAddins<T>() where T : IBookvarAddin
        {
            Collection<AddInToken> tokens = AddInStore.FindAddIns( typeof ( T ), addInRoot );
            foreach (AddInToken token in tokens)
            {
                yield return token.Activate<T>( AddInSecurityLevel.FullTrust );
            }
        }

        public void RebuildAddinStore()
        {
            AddInStore.Rebuild( addInRoot );
        }
    }
}
